﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LTP.TextEditor;
using System.IO;

namespace AtfutureCodeMachine
{
    public partial class FormShowCode : Form
    {
        public FormShowCode()
        {
            InitializeComponent();

            this.Shown += new EventHandler(FormShowCode_Shown);
            this.tabControl1.DoubleClick += new EventHandler(tabControl1_DoubleClick);
        }

        void tabControl1_DoubleClick(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex != tabControl1.TabCount - 1)
            {
                TextEditorControl codeview = this.tabControl1.SelectedTab.Controls[0] as TextEditorControl;
                string vm = this.tabControl1.SelectedTab.Tag.ToString();

                FormMain.g_code[vm] = ViewTemplate.ParseVm(vm);
                codeview.Text = FormMain.g_code[vm];
            }
        }

        void FormShowCode_Shown(object sender, EventArgs e)
        {
            //移除默认的
            this.tabControl1.TabPages.RemoveAt(0);
            this.tabControl1.TabPages.RemoveAt(0);

            foreach (string s in FormMain.g_code.Keys)
            {
                this.tabControl1.TabPages.Add(s.ToUpper());
                TextEditorControl codeview = new TextEditorControl();
                codeview.Dock = DockStyle.Fill;                

                this.tabControl1.TabPages[this.tabControl1.TabCount - 1].Controls.Add(codeview);
                this.tabControl1.TabPages[this.tabControl1.TabCount - 1].Tag = s.ToUpper();

                string strExt = s.Split('.')[1].ToLower();
                if (strExt == "vm" || strExt == "htm" || strExt == "html" || strExt == "aspx" || strExt == "ashx") codeview.Language = TextEditorControlBase.Languages.HTML;
                if (strExt == "hbm" || strExt == "xml") codeview.Language = TextEditorControlBase.Languages.XML;
                if (strExt == "sql") codeview.Language = TextEditorControlBase.Languages.SQL;
                if (strExt == "vb") codeview.Language = TextEditorControlBase.Languages.VBNET;
                if (strExt == "js") codeview.Language = TextEditorControlBase.Languages.JavaScript;
                if (strExt == "java") codeview.Language = TextEditorControlBase.Languages.Java;
                if (strExt == "cs") codeview.Language = TextEditorControlBase.Languages.CSHARP;

                codeview.Text = FormMain.g_code[s.ToUpper()];
            }

            //加载说明
            TextEditorControl te = new TextEditorControl();
            te.Dock = DockStyle.Fill;
            this.tabControl1.TabPages.Add("Help");
            this.tabControl1.TabPages[this.tabControl1.TabCount - 1].Controls.Add(te);

            te.Language = TextEditorControlBase.Languages.CSHARP;
            using (StreamReader sr = new StreamReader(Path.Combine(Application.StartupPath, "help.vm"),
                Encoding.GetEncoding("gb2312")))
            {
                te.Text = sr.ReadToEnd();
            }
        }

        /// <summary>
        /// 返回值
        /// </summary>
        public string Value
        {
            set
            {
                //this.richTextBox1.Text = value;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
